/**
 * forms service, 用于获取布局和布局下的表单元素
 * @author werts
 */
import { Injectable } from '@angular/core';
import { Headers, Http, Response } from '@angular/http';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';

import { FormsElementsBase } from './class.forms.elements.base';
import { Forms } from './class.forms';

@Injectable()
export class FormsService {
    private header = new Headers({ 'Content-Type': 'application/json' });

    private formsUrl = 'api/forms';

    constructor(private http: Http) { }

    getFormElements(type: string) {
        let formElements: FormsElementsBase<any>[];

        return formElements;
    }
    getFormAndFormElements(): Observable<Forms> {
        return this.http.get(this.formsUrl).map(this.extractData).catch(this.handleError);
    }

    private extractData(resp: Response) {
        let body = resp.json();

        return body.data || {};
    }

    /**
     * 错误和异常处理信息
     */
    private handleError(error: Response | any) {
        return Observable.throw(error);
    }
}